package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.service.IAssignRecordService;
import com.ruoyi.crm.service.IBusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.ClueMapper;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.service.IClueService;

import javax.annotation.Resource;

/**
 * 线索Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-13
 */
@Service
public class ClueServiceImpl implements IClueService
{
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private IBusinessService businessService;
    @Resource
    private IAssignRecordService assignRecordService;

    /**
     * 查询线索
     * 
     * @param id 线索主键
     * @return 线索
     */
    @Override
    public Clue selectClueById(Long id)
    {
        return clueMapper.selectClueById(id);
    }

    /**
     * 查询线索列表
     * 
     * @param clue 线索
     * @return 线索
     */
    @Override
    public List<Clue> selectClueList(Clue clue)
    {
        return clueMapper.selectClueList(clue);
    }

    /**
     * 新增线索
     * 
     * @param clue 线索
     * @return 结果
     */
    @Override
    public int insertClue(Clue clue)
    {
         clue.setCreateBy(SecurityUtils.getUsername());
        clue.setCreateTime(DateUtils.getNowDate());
        return clueMapper.insertClue(clue);
    }

    /**
     * 修改线索
     * 
     * @param clue 线索
     * @return 结果
     */
    @Override
    public int updateClue(Clue clue)
    {
        clue.setUpdateBy(SecurityUtils.getUsername());
        clue.setUpdateTime(DateUtils.getNowDate());
        return clueMapper.updateClue(clue);
    }

    /**
     * 批量删除线索
     * 
     * @param ids 需要删除的线索主键
     * @return 结果
     */
    @Override
    public int deleteClueByIds(Long[] ids)
    {
        return clueMapper.deleteClueByIds(ids);
    }

    /**
     * 删除线索信息
     * 
     * @param id 线索主键
     * @return 结果
     */
    @Override
    public int deleteClueById(Long id)
    {
        return clueMapper.deleteClueById(id);
    }

    /**
     * 伪线索上报
     *1.falseCount最大值为3
     * 2.状态为 伪线索
     * @param id
     * @return
     */
    @Override
    public int falseClue(Long id) {
        Clue clue = this.selectClueById(id);
        if(clue.getFalseCount()>=Clue.FALSE_COUNT_LIMIT){
         throw new RuntimeException("伪线索次数超限");
        }
        clue.setStatus(Clue.Status.FALSE.getValue());
        clue.setFalseCount(clue.getFalseCount()+1);
        return this.updateClue(clue);
    }


    /**
     * 1.更新线索
     * 2.插入商机
     * 3.分配商机给当前用户
     * @param id
     * @return
     */
    @Override
    public int transformToBusiness(Long id) {
        Clue clue = this.selectClueById(id);
        clue.setStatus(Clue.Status.TO_BUSINESS.getValue());
        this.updateClue(clue);

        //2.插入商机
        Business business=new Business();
        BeanUtils.copyProperties(clue,business);
        business.setStatus(Clue.Status.NEW.getValue());
        return businessService.insertBusiness(business);
    }

    /**
     * 重新捞取线索
     * 1.状态修改成已经分配
     * 2.插入分配记录
     * @param id
     * @return
     */
    @Override
    public int resetClue(Long id) {
        //插入分配记录
        AssignRecord assignRecord=new AssignRecord();
        assignRecord.setType(AssignRecord.Type.CLUE.name());
        assignRecord.setAssignId(id);
        assignRecord.setUserId(SecurityUtils.getUserId());
        assignRecord.setUserName(SecurityUtils.getUsername());
     return   assignRecordService.insertAssignRecord(assignRecord);
    }

    @Override
    public int uploadClues(List<Clue> clues) {
         int sum=0;
        for (Clue clue : clues) {
            //转换字典数据
            clue.setChannel(DictUtils.getDictValue("clues_item",clue.getChannel()));
            clue.setStatus(DictUtils.getDictLabel("sys_user_sex",clue.getSex()));
            clue.setLevel(DictUtils.getDictValue("clues_level",clue.getLevel()));
            clue.setSubject(DictUtils.getDictLabel("course_subject",clue.getSubject()));
            clue.setStatus(Clue.Status.NEW.getValue());
            sum+=this.insertClue(clue);
        }
         return sum;

    }
}
